package cn.yunhe.controller;

import cn.yunhe.pojo.ResultRequest;
import cn.yunhe.pojo.ResultResponse;
import cn.yunhe.pojo.UserInfo;
import cn.yunhe.service.IUserInfoService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @Autowired
    IUserInfoService userInfoService;

    @Autowired
    BCryptPasswordEncoder passwordEncoder;

    @RequestMapping("/queryAll")
    public ResultResponse queryAll(@RequestBody ResultRequest resultRequest){
        List<UserInfo> userInfoList = userInfoService.queryAll(resultRequest);
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(userInfoList);
        resultResponse.setCount(new PageInfo(userInfoList).getTotal());
        return  resultResponse;
    }

    @RequestMapping("/queryDetail")
    public ResultResponse queryUserInfoDetail(String id){
        UserInfo userInfo = userInfoService.queryUserInfoDetail(id);
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(userInfo);
        return resultResponse;
    }

    @RequestMapping("/insert")
    public ResultResponse insertUserInfo(@RequestBody UserInfo userInfo){
        String passwordEncode = passwordEncoder.encode(userInfo.getPassword());
        userInfo.setPassword(passwordEncode);
        userInfoService.insertUserInfo(userInfo);
        return new ResultResponse();
    }

    @RequestMapping("/insertUserRoles")
    public ResultResponse insertUserRoles(String userId,String[] ids){
        userInfoService.insertUserRoles(userId, ids);
        return new ResultResponse();
    }

}
